﻿using System.Collections.Generic;
using System.Linq;
using MessageHandler;

namespace Chatclient
{
	public class PrivateChatMembers
	{
		UserInfo thisUser;

		public UserInfo ThisUser
		{
			get { return thisUser; }
			set { thisUser = value; }
		}

		List<UserInfo> userInfos = new List<UserInfo>();

		public List<UserInfo> ChatMembers
		{
			get { return userInfos; }
		}

		public PrivateChatMembers(UserInfo[] userInfos, UserInfo thisUser)
		{
			this.thisUser = thisUser;
			this.AddRange(userInfos);
		}

		private void RemoveThisUser()
		{
			if (userInfos.Contains(this.ThisUser))
			{
				Remove(this.ThisUser);
			}
		}

		public void Add(UserInfo userInfo)
		{
			userInfos.Add(userInfo);
			userInfos.Sort();
		}

		public void AddRange(UserInfo[] userInfos)
		{
			this.userInfos.AddRange(userInfos.ToList<UserInfo>());
			RemoveThisUser();
			this.userInfos.Sort();
		}

		public void Remove(UserInfo userInfo)
		{
			userInfos.Remove(userInfo);
		}

		public override bool Equals(object obj)
		{
			if (obj is PrivateChatMembers)
			{
				PrivateChatMembers tmp = (PrivateChatMembers)obj;
				return userInfos.Equals(tmp.userInfos);
			}
			return false;
		}

		public override int GetHashCode()
		{
			List<UserInfo> tmpInfos = new List<UserInfo>(this.userInfos);
			tmpInfos.Add(this.ThisUser);
			tmpInfos.Sort();
			string output = string.Empty;
			foreach (UserInfo info in userInfos)
			{
				output += info.ToString();
			}
			return output.GetHashCode();
		}

		public override string ToString()
		{
			string output = string.Empty;
			foreach (UserInfo info in userInfos)
			{
				output += info.ToString() + ", ";
			}
			return output.Substring(0, output.Length - 2);
		}

		internal string PrintUsers()
		{
			string output = string.Empty;
			foreach (var user in userInfos)
			{
				output += user.ToString() + "-";
			}
			return output.Substring(0, output.Length - 1);
		}
	}
}
